package cn.stylefeng.guns.modular.gd.service.impl;

import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.stylefeng.guns.modular.gd.entity.GdTools;
import cn.stylefeng.guns.modular.gd.service.GdToolsService;
import cn.stylefeng.guns.modular.gd.mapper.GdToolsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 24122
* @description 针对表【t_gd_tools】的数据库操作Service实现
* @createDate 2023-03-31 15:00:26
*/
@Service
public class GdToolsServiceImpl extends ServiceImpl<GdToolsMapper, GdTools>
    implements GdToolsService{
    @Resource
    GdToolsMapper gdToolsMapper;


    @Override
    public ResponseData getList() {
        List<GdTools> list = gdToolsMapper.selectList(null);
        return new SuccessResponseData(list);
        //return new ResponseData<List<GdTools>>(true,"0","success",gdTools);
    }

    @Override
    public ResponseData update(GdTools tools) {
        int i = gdToolsMapper.updateById(tools);
        if(i>0){
            return new SuccessResponseData();
        }
        return new ErrorResponseData("1","update fail");
    }

    @Override
    public ResponseData delTool(Integer id) {
        int i = gdToolsMapper.deleteById(id);
        if(i>0){
            return new SuccessResponseData();
        }
        return new ErrorResponseData("1","失败");
    }

    @Override
    public ResponseData name(String name) {
        QueryWrapper<GdTools> gdToolsQueryWrapper = new QueryWrapper<>();
        gdToolsQueryWrapper.eq("tname",name);
        GdTools gdTools = gdToolsMapper.selectOne(gdToolsQueryWrapper);
        return new SuccessResponseData(gdTools);
    }

    @Override
    public ResponseData add(GdTools tools) {
        if(gdToolsMapper.selectOne(new QueryWrapper<GdTools>().eq("tname",tools.getTname()))!=null){
            return new ErrorResponseData("1","已有该工具，不可重复");
        }
        int insert = gdToolsMapper.insert(tools);
        if(insert>0){
            return new SuccessResponseData();
        }
        return new ErrorResponseData("1","新增失败");
    }

    @Override
    public ResponseData getNames() {
        List<String> names = gdToolsMapper.getNames();

        return new SuccessResponseData(names);
    }
}




